package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.BaseBean;
import com.infore.airsupervise.bean.EventDetailsE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.impl.EventBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.fragment.ShowPhotoFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Administrator on 2017/8/21.
 */

public class EventDetailsActivity extends BaseActivity {

    @BindView(R.id.titlebar_left_menu)
    AwesomeTextView titlebarLeftMenu;
    @BindView(R.id.titlebar_title)
    TextView titlebarTitle;
    @BindView(R.id.titlebar_right_menu)
    AwesomeTextView titlebarRightMenu;


    @BindView(R.id.eventdetails_item_text)
    TextView mEventTitle;
    @BindView(R.id.eventdetails_stationname)
    TextView mEventStationName;

    @BindView(R.id.eventdetails_happen_time)
    TextView mEventHappenTime;
    @BindView(R.id.eventdetails_pollution_factor)
    TextView mPollutionFactor;

    @BindView(R.id.eventdetails_critical)
    TextView mEventCritical;
    @BindView(R.id.eventdetails_task_create)
    TextView mEventTaskPerson;
    @BindView(R.id.eventdetails_task_handler)
    TextView mEventTaskHandler;
    @BindView(R.id.event_textdesc_text)
    TextView mEventDesc;

    @BindView(R.id.event_handler_info_layout)
    LinearLayout mEventInfo_Handler_Layout;
    @BindView(R.id.eventdetails_handler_time)
    TextView eventdetailsHandlerTime;
    @BindView(R.id.eventdetails_handler_person)
    TextView eventdetailsHandlerPerson;
    @BindView(R.id.event_handler_record)
    TextView eventHandlerRecord;
//    @BindView(R.id.event_handlercurrent_image)
//    ImageView eventHandlercurrentImage;
    @BindView(R.id.eventdetails_submit_location)
    TextView eventdetailsSubmitLocation;
    @BindView(R.id.eventdetails_item_title)
    TextView eventdetailsItemTitle;
    @BindView(R.id.event_title)
    TextView eventTitle;
    private EventBizImpl eventBiz;
    private SweetAlertDialog dialog;
    private String eventid;
    private ShowPhotoFragment showPhotoFragment;
    private String flag;

    private boolean eventnotrequest=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_details);
        eventBiz = new EventBizImpl();
        ButterKnife.bind(this);
        initview();
    }

    private void initview() {
        eventid = getIntent().getStringExtra("eventid");
        titlebarLeftMenu.setFontAwesomeIcon(FontAwesome.FA_CHEVRON_LEFT);
        flag = getIntent().getStringExtra("flag");
        if(!flag.equals("2")){
            titlebarRightMenu.setText("处理");

        }else{
            titlebarRightMenu.setVisibility(View.INVISIBLE);
        }

        LoadEventDetails();

    }

    private void LoadEventDetails() {

        dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.show();
        eventBiz.getEventDetails(eventid, new OnServerResponseListener<EventDetailsE>() {
            @Override
            public void onSucceed(EventDetailsE result, String desc) {
                Log.e("EventDetailsE", result.toString());
                if (result.getProcessList() != null && result.getProcessList().size() > 0) {
                    mEventInfo_Handler_Layout.setVisibility(View.VISIBLE);
                } else {
                    mEventInfo_Handler_Layout.setVisibility(View.GONE);
                }
                titlebarTitle.setText(result.getEventName());
                mEventTitle.setText(result.getEventName());
                mEventStationName.setText(result.getStationName());
                mEventHappenTime.setText(result.getEventTime());
                mPollutionFactor.setText(result.getFactor());
                mEventCritical.setText(result.getPoll_degree());
                mEventTaskPerson.setText(result.getReporter());
                mEventTaskHandler.setText(result.getDispatcher());
                mEventDesc.setText(result.getDescriptions());
                List<EventDetailsE.ProcessListBean> bean= result.getProcessList();
                if(bean!=null&&bean.size()>0){

                    eventdetailsHandlerTime.setText(bean.get(0).getProcessTime());
                    eventdetailsHandlerPerson.setText(result.getDispatcher());
                    eventHandlerRecord.setText(bean.get(0).getDescribe());
                    List<String> path=bean.get(0).getPath();
                    if(path!=null&&path.size()>0){
                        if (showPhotoFragment == null) {
                            showPhotoFragment = new ShowPhotoFragment(path, 200);
                        }
                        getSupportFragmentManager().beginTransaction().replace(R.id.event_handlercurrent_image, showPhotoFragment)
                                .commit();
                    }

                    eventdetailsSubmitLocation.setText(bean.get(0).getAddress());

                    mEventInfo_Handler_Layout.setVisibility(View.VISIBLE);
                }
                //当前用户ID和服务器返回的处理人ID 相等
                Log.e("UserID",UserE.getUserInfo().getUserId());
                Log.e("DispatcherId",result.getDispatcherId());
                if(UserE.getUserInfo().getUserId().equals(result.getDispatcherId())){
                      //事件状态 为处理中
                      if(flag.equals("1")){
                          eventnotrequest=true;  //点击跳转的时候不用发送请求
                      }
                }else{
                    titlebarRightMenu.setVisibility(View.GONE);
                }

                dialog.dismiss();

            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                dialog.setTitleText(title)
                        .setContentText(desc)
                        .setConfirmText(MsgConst.I_SEE)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
            }
        });

    }

    @OnClick(R.id.titlebar_right_menu)
    void handlerEvent() {

        if(!eventnotrequest){

        dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.show();

        Log.e("onSucceed--id", eventid + "--");
        eventBiz.getHandlerEventVerify(eventid, new OnServerResponseListener<BaseBean>() {
            @Override
            public void onSucceed(BaseBean result, String desc) {
                int status = result.getStatus();
                if (status == 1) {
                    Toast.makeText(EventDetailsActivity.this, "成功：" + result.getMessage(), Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(EventDetailsActivity.this, SubmitHandlerEventActivity.class);
//                    intent.putExtra("eventtitle", mEventTitle.getText().toString());
//                    intent.putExtra("eventstation", mEventStationName.getText().toString());
//                    intent.putExtra("eventtime", mEventHappenTime.getText().toString());
//                    intent.putExtra("eventperson", mEventTaskHandler.getText());
//                    intent.putExtra("eventid", eventid);
//                    startActivity(intent);
                        HandleEventlunch();//跳转
                } else {
                    Toast.makeText(EventDetailsActivity.this, "失败：" + result.getMessage(), Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                dialog.setTitleText(title)
                        .setContentText(desc)
                        .setConfirmText(MsgConst.I_SEE)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
            }
        });

        }else{

            HandleEventlunch();//跳转
        }
    }

    private void HandleEventlunch(){
        Intent intent = new Intent(EventDetailsActivity.this, SubmitHandlerEventActivity.class);
        intent.putExtra("eventtitle", mEventTitle.getText().toString());
        intent.putExtra("eventstation", mEventStationName.getText().toString());
        intent.putExtra("eventtime", mEventHappenTime.getText().toString());
        intent.putExtra("eventperson", mEventTaskHandler.getText());
        intent.putExtra("eventid", eventid);
        startActivity(intent);
        myFinish();
    }

    /**
     * 返回上个界面
     */
    @OnClick(R.id.titlebar_left_menu)
    void leftMenuClick() {
        finish();
    }

    private void myFinish(){
        setResult(RESULT_OK);
        finish();
    }

//    @Override
//    public void onBackPressed() {
//        myFinish();
//    }
}
